package com.acmen.easyapi.model;

import com.alibaba.fastjson.JSON;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试执行结果
 *
 * @author acmen
 */
public class CallResult {
    /**
     * ID
     */
    private String id;
    /**
     * ID
     */
    private String reportId;

    /**
     * 接口ID
     */
    private String apiId;

    /**
     * 测试用例名
     */
    private String testName;

    /**
     * 花费时间
     */
    private long cost;
    /**
     * 发送流量（字节）
     */
    private long sendSize;

    /**
     * 发送流量（字节）
     */
    private long receiveSize;


    /**
     * 开始时间
     */
    private LocalDateTime beginTime;

    public String headers;//请求头部数据
    public String params;//请求参数数据
    public String body;//请求body数据
    private String expectResponse;

    public String result;//响应数据

    /**
     * 响应成功
     */
    private boolean responseSuccess;

    /**
     * 业务成功
     */
    private boolean businessSuccess;

    /**
     * 格式化字节
     * @param size
     */
    public static String formatByte(long size) {
        //获取到的size为：1705230
        int GB = 1024 * 1024 * 1024;//定义GB的计算常量
        int MB = 1024 * 1024;//定义MB的计算常量
        int KB = 1024;//定义KB的计算常量
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数
        String resultSize = "";
        if (size / GB >= 1) {
            //如果当前Byte的值大于等于1GB
            resultSize = df.format(size / (float) GB) + "GB   ";
        } else if (size / MB >= 1) {
            //如果当前Byte的值大于等于1MB
            resultSize = df.format(size / (float) MB) + "MB   ";
        } else if (size / KB >= 1) {
            //如果当前Byte的值大于等于1KB
            resultSize = df.format(size / (float) KB) + "KB   ";
        } else {
            resultSize = size + "B   ";
        }
        return resultSize;
    }


    public String getSendSizeString() {
        return formatByte(sendSize);
    }
    public String getReceiveSizeString() {
        return formatByte(receiveSize);
    }

    public String getRequestData(){
        Map requestData = new HashMap<>();
        if(headers!= null && !headers.equals("")){
            requestData.put("headers", JSON.parse(headers));
        }
        if(params!= null && !params.equals("")){
            requestData.put("params", JSON.parse(params));
        }
        if(body!= null && !body.equals("")){
            requestData.put("body", JSON.parse(body));
        }
        return JSON.toJSONString(requestData);
    }


    public String getId() {
        return id;
    }

    public CallResult setId(String id) {
        this.id = id;
        return this;
    }

    public String getReportId() {
        return reportId;
    }

    public CallResult setReportId(String reportId) {
        this.reportId = reportId;
        return this;
    }

    public String getApiId() {
        return apiId;
    }

    public CallResult setApiId(String apiId) {
        this.apiId = apiId;
        return this;
    }

    public String getTestName() {
        return testName;
    }

    public CallResult setTestName(String testName) {
        this.testName = testName;
        return this;
    }

    public long getCost() {
        return cost;
    }

    public CallResult setCost(long cost) {
        this.cost = cost;
        return this;
    }

    public long getSendSize() {
        return sendSize;
    }

    public CallResult setSendSize(long sendSize) {
        this.sendSize = sendSize;
        return this;
    }

    public long getReceiveSize() {
        return receiveSize;
    }

    public CallResult setReceiveSize(long receiveSize) {
        this.receiveSize = receiveSize;
        return this;
    }

    public LocalDateTime getBeginTime() {
        return beginTime;
    }

    public CallResult setBeginTime(LocalDateTime beginTime) {
        this.beginTime = beginTime;
        return this;
    }

    public String getHeaders() {
        return headers;
    }

    public CallResult setHeaders(String headers) {
        this.headers = headers;
        return this;
    }

    public String getParams() {
        return params;
    }

    public CallResult setParams(String params) {
        this.params = params;
        return this;
    }

    public String getBody() {
        return body;
    }

    public CallResult setBody(String body) {
        this.body = body;
        return this;
    }

    public String getExpectResponse() {
        return expectResponse;
    }

    public CallResult setExpectResponse(String expectResponse) {
        this.expectResponse = expectResponse;
        return this;
    }

    public String getResult() {
        return result;
    }

    public CallResult setResult(String result) {
        this.result = result;
        return this;
    }

    public boolean isResponseSuccess() {
        return responseSuccess;
    }

    public CallResult setResponseSuccess(boolean responseSuccess) {
        this.responseSuccess = responseSuccess;
        return this;
    }

    public boolean isBusinessSuccess() {
        return businessSuccess;
    }

    public CallResult setBusinessSuccess(boolean businessSuccess) {
        this.businessSuccess = businessSuccess;
        return this;
    }
}
